package testng.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.citybrain.flow.platform.manager.FlowEngineApplication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
 * @author cheng.huaxing
 * @date 2019-03-13
 */
@SpringBootTest(classes = FlowEngineApplication.class)
public class FlowFormControllerTest extends AbstractTransactionalTestNGSpringContextTests {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @BeforeClass
    public void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    }

    //@Test(dataProvider = "formCreate")
    public void testCreate(String formCreate) throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/create").characterEncoding("UTF-8")
                .content(formCreate).contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testFind() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/find").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"formId\":\"495001\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test(dataProvider = "formSave")
    public void testSave(String formSave) throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/save").characterEncoding("UTF-8")
                .content(formSave).contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testDetail() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/detail").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"formId\":\"495001\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testDeploymentDetail() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/deployment/detail").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"formDeployId\":\"1105044\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testDelete() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/delete").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"formId\":\"1042510\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    //@Test
    public void testList() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders
                .post("/form/list").characterEncoding("UTF-8")
                .content("{\n" +
                        "  \"pageNum\":1,\n" +
                        "  \"pageSize\":10,\n" +
                        "  \"searchName\":\"\",\n" +
                        "  \"searchKey\":\"\",\n" +
                        "  \"timeStart\":\"2019-04-01\",\n" +
                        "  \"timeEnd\":\"2019-04-12\"\n" +
                        "}").contentType("application/json"))
                .andDo(MockMvcResultHandlers.print())
                .andExpect(MockMvcResultMatchers.status().isOk())
                .andReturn();
        String content = result.getResponse().getContentAsString();
        JSONObject jsonObject = JSON.parseObject(content);
        Assert.assertEquals(jsonObject.get("errCode"), 0);
    }

    @DataProvider(name = "formCreate")
    public Object[][] provideFormCreate() {
        return new Object[][] {{"{\n" +
                "    \n" +
                "    \"name\":\"表单测试8\",\n" +
                "    \"key\":\"formtest88\",\n" +
                "    \"description\":\"\",\n" +
                "    \"version\":1,\n" +
                "    \"fields\":[\n" +
                "        {\n" +
                "            \"fieldType\":\"OptionFormField\",\n" +
                "            \"id\":\"label\",\n" +
                "            \"name\":\"Label\",\n" +
                "            \"type\":\"dropdown\",\n" +
                "            \"value\":\"1\",\n" +
                "            \"required\":false,\n" +
                "            \"readOnly\":false,\n" +
                "            \"overrideId\":false,\n" +
                "            \"placeholder\":null,\n" +
                "            \"layout\":null,\n" +
                "            \"optionType\":null,\n" +
                "            \"hasEmptyValue\":true,\n" +
                "            \"options\":[\n" +
                "                {\n" +
                "                    \"id\":null,\n" +
                "                    \"name\":\"请选择一个...\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"id\":null,\n" +
                "                    \"name\":\"1\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"optionsExpression\":null,\n" +
                "            \"params\":{\n" +
                "\n" +
                "            }\n" +
                "        }\n" +
                "    ],\n" +
                "    \"outcomes\":[\n" +
                "\n" +
                "    ]\n" +
                "}"}};
    }

    @DataProvider(name = "formSave")
    public Object[][] provideFormSave() {
        return new Object[][] {{"{\n" +
                "    \"id\":\"507503\",\n" +
                "    \"name\":\"表单测试8\",\n" +
                "    \"key\":\"formtest8\",\n" +
                "    \"description\":\"\",\n" +
                "    \"version\":1,\n" +
                "    \"fields\":[\n" +
                "        {\n" +
                "            \"fieldType\":\"OptionFormField\",\n" +
                "            \"id\":\"label\",\n" +
                "            \"name\":\"Label\",\n" +
                "            \"type\":\"dropdown\",\n" +
                "            \"value\":\"1\",\n" +
                "            \"required\":false,\n" +
                "            \"readOnly\":false,\n" +
                "            \"overrideId\":false,\n" +
                "            \"placeholder\":null,\n" +
                "            \"layout\":null,\n" +
                "            \"optionType\":null,\n" +
                "            \"hasEmptyValue\":true,\n" +
                "            \"options\":[\n" +
                "                {\n" +
                "                    \"id\":null,\n" +
                "                    \"name\":\"请选择一个...\"\n" +
                "                },\n" +
                "                {\n" +
                "                    \"id\":null,\n" +
                "                    \"name\":\"1\"\n" +
                "                }\n" +
                "            ],\n" +
                "            \"optionsExpression\":null,\n" +
                "            \"params\":{\n" +
                "\n" +
                "            }\n" +
                "        }\n" +
                "    ],\n" +
                "    \"outcomes\":[\n" +
                "\n" +
                "    ]\n" +
                "}"}};
    }
}